package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.WeekPlanProjectAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.WeekPlanProject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wu on 2016/5/5.
 * 计划项目
 */
public class WeekPlanProjectFragment extends BaseListFragment {

  public static final String EXTRA_NAME =
      "com.isunland.managesystem.ui.WeekPlanProjectFragment.EXTRA_NAME";
  public static final String EXTRA_ID =
      "com.isunland.managesystem.ui.WeekPlanProjectFragment.EXTRA_ITEM";
  private WeekPlanProjectAdapter mAdapter;
  private String mCustomerId;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.projectList);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_PLAN_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<String, String>();
    String memberCode = CurrentUser.newInstance(getActivity()).getMemberCode();
    params.put("curPage", "1");
    params.put("pageSize", "10000");
    params.put("memberCode", memberCode);
    params.put("IfAcceptance", "F");
    params.put("ifAllpay", "F");
    params.put("dataStatus", "publish");
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("orderSeq", "desc");

    //        params.put("type", "mobile");

    return params;
  }

  @Override public void resolveResponse(String json) {
    WeekPlanProject attendanceListOriginal = new Gson().fromJson(json, WeekPlanProject.class);
    List<WeekPlanProject.Project> rows = attendanceListOriginal.getRows();
    setAdapter(rows);
  }

  public void setAdapter(List<WeekPlanProject.Project> detail) {
    if (detail == null) {
      detail = new ArrayList<WeekPlanProject.Project>();
    }

    if (mAdapter == null) {
      mAdapter = new WeekPlanProjectAdapter(getActivity(), detail);
      setListAdapter(mAdapter);
      return;
    }

    mAdapter.clear();
    mAdapter.addAll(detail);
    ((WeekPlanProjectAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    WeekPlanProject.Project project = mAdapter.getItem(position - 1);
    String projectId = project.getId();
    String name = project.getProjectName();
    Intent intent = new Intent();
    intent.putExtra(EXTRA_ID, projectId);
    intent.putExtra(EXTRA_NAME, name);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }
}
